package com.lcb.order.service.impl;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.lcb.common.utils.BaseApiService;
import com.lcb.common.utils.BaseResponse;
import com.lcb.order.entity.TAccount;
import com.lcb.order.entity.TOrder;
import com.lcb.order.entity.TStorage;
import com.lcb.order.mapper.TOrderMapper;
import com.lcb.order.service.AccountService;
import com.lcb.order.service.StroageService;
import com.lcb.order.service.TOrderService;
import io.seata.spring.annotation.GlobalTransactional;
import lombok.extern.slf4j.Slf4j;
import org.checkerframework.checker.units.qual.A;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RestController;
import tk.mybatis.mapper.entity.Example;

import java.math.BigDecimal;
import java.util.Arrays;

/****
 * @Author:lcb
 * @Description:TOrder业务层接口实现类
 * @Date: 2021-01-28 11:24:58
 *****/

@RestController
//@RequestMapping("/order")
//@CrossOrigin
@Slf4j
public class TOrderServiceImpl extends BaseApiService implements TOrderService {

    @Autowired
    private TOrderMapper tOrderMapper;

    @Autowired
    private StroageService stroageService;

    @Autowired
    private AccountService accountService;

    /**
     * TOrder条件+分页查询
     *
     * @param tOrder 查询条件
     * @param page   页码
     * @param size   页大小
     * @return 分页结果
     */
    @Override
    public BaseResponse findTOrderList(@RequestBody TOrder tOrder, @PathVariable(value = "page") int page, @PathVariable(value = "size") int size) {
        //分页
        PageHelper.startPage(page, size);

        PageInfo<TOrder> pagelist = new PageInfo<TOrder>(tOrderMapper.select(tOrder));

        return setResultSuccess(pagelist);
    }

    /**
     * 删除
     *
     * @param ids
     */
    @Override
    public BaseResponse deleteTOrderById(@PathVariable(value = "ids") String ids) {

        String[] split = ids.split(",");
        Example example = new Example(TOrder.class);
        Example.Criteria criteria = example.createCriteria();
        criteria.andIn("id", Arrays.asList(split));
        tOrderMapper.deleteByExample(example);
        return setResultSuccess("删除成功");
    }

    /**
     * 修改TOrder
     *
     * @param tOrder
     */
    @Override
    public BaseResponse updateTOrder(@RequestBody TOrder tOrder) {
        tOrderMapper.updateByPrimaryKeySelective(tOrder);
        return setResultSuccess("修改成功");
    }

    /**
     * 增加TOrder
     *
     * @param tOrder
     */
    @GlobalTransactional(rollbackFor = Exception.class)
    @Override
    public BaseResponse addTOrder(@RequestBody TOrder tOrder) {

        log.info("------>开始新建订单");
        tOrderMapper.insertSelective(tOrder);

        log.info("------>订单服务开始调用库存服务,做扣减");
        TStorage tStorage = new TStorage();
        stroageService.decrease(tStorage);

        log.info("------>订单服务开始调用账户服务,做扣减");
        TAccount tAccount = new TAccount();
        accountService.decrease(tAccount);

        log.info("修改订单状态");
        tOrder.setStatus(1);
        tOrderMapper.updateByPrimaryKeySelective(tOrder);

        log.info("下订单结束");
        return setResultSuccess("添加成功");
    }

    /**
     * 根据ID查询TOrder
     *
     * @param id
     * @return
     */
    @Override
    public BaseResponse findTOrderById(@PathVariable(value = "id") Long id) {
        return setResultSuccess(tOrderMapper.selectByPrimaryKey(id));
    }

    /**
     * 查询TOrder全部数据
     *
     * @return
     */
    @Override
    public BaseResponse findTOrderAll() {
        return setResultSuccess(tOrderMapper.selectAll());
    }
}
